#!/bin/sh

. /usr/share/common

CNET=100.64.1

WPA_AP_CONF="/etc/wpa-ap.conf"

# Define net_dev in the global scope
if [ "hi3881" = "$wlan_module" ]; then
	net_dev="ap0"
else
	net_dev="wlan0"
fi
sed -i "s/%dev%/$net_dev/g" /etc/udhcpd-ap.conf

start() {
	echo_title "WLAN Access Point"

	if [ -f "$PORTAL_MODE_FLAG" ]; then
		echo_error "Portal mode enabled"
		exit 1
	fi

	if iface_exists "eth0"; then
		echo_error "Wired interface found"
		exit 1
	fi

	if iface_exists "usb0"; then
		echo_error "Wired interface found"
		exit 1
	fi

	if ! iface_exists "$net_dev"; then
		echo_error "Wireless interface not found"
		exit 1
	fi

	# quit if credentials are set, AP conflicts with normal wifi operation

	if [ "true" != "$wlanap_enabled" ]; then
		echo_error "WLAN Access Point disabled"
		exit 1
	fi

	echo_info "Create DHCP leases database"
	touch /var/run/udhcpd_ap.leases

	echo_info "Set WLAN AP resolver configuration"
	cp /etc/resolv-ap.conf /tmp/resolv.conf

	echo_info "Add IP address to $net_dev"
	ip a add dev $net_dev ${CNET}.1/24

	echo_info "Bring $net_dev up"
	ip link set $net_dev up

	echo_info "Add route to $net_dev"
	ip route add ${CNET}.0/24 dev $net_dev > /dev/null 2>&1

	echo_info "Set WLAN AP DNS configuration"
	cp /etc/dnsd-ap.conf /tmp/dnsd.conf

	hostname=$(hostname)

	if ! grep -q $hostname /tmp/dnsd.conf >/dev/null; then
		echo_info "Add $hostname to DNS configuration"
		echo $hostname.local ${CNET}.1 >> /tmp/dnsd.conf
	fi

	echo_info "Start udhcpd"
	start-stop-daemon -S -x /sbin/udhcpd -- -S -I ${CNET}.1 /etc/udhcpd-ap.conf

	echo_info "Start dnsd"
	start-stop-daemon -S -x /sbin/dnsd -- -i ${CNET}.1 -c /tmp/dnsd.conf -d

	if [ -z "$wlanap_ssid" ]; then
		echo_info "Generate WLAN AP SSID"
		wlanap_ssid="THINGINO-$(iface_mac_suffix $net_dev)"
	fi

	echo_info "Update WLAN AP SSID in $WPA_AP_CONF to $wlanap_ssid"
	sed -E -i "/^\s+ssid=/c\\\tssid=\"$wlanap_ssid\"" $WPA_AP_CONF

	if [ ${#wlanap_pass} -lt 8 ]; then
		echo_info "Use default WLAN AP password"
		wlanap_pass="thingino"
	fi

	if [[ ${#wlanap_pass} -eq 64 && "$wlanap_pass" =~ ^[0-9a-fA-F]{64}$ ]]; then
		echo_info "WLAN AP password is a PSK"
		quote=''
	else
		quote='"'
	fi
	sed -i '/psk=/ s/psk=["]*[^"]*["]*/psk='"$quote$wlanap_pass$quote"'/' $WPA_AP_CONF

	echo_info "Start wpa_supplicant"
	start-stop-daemon -S -x /sbin/wpa_supplicant -- -i $net_dev -B -c $WPA_AP_CONF

	echo_warning "WLAN Access Point started"
	touch "$WLANAP_MODE_FLAG"
}

stop() {
	echo_title "WLAN Access Point"

	echo_info "Stop udhcpd"
	start-stop-daemon -K -q -x /sbin/udhcpd

	echo_info "Stop dnsd"
	start-stop-daemon -K -q -x /sbin/dnsd

	echo_info "Stop wpa_supplicant"
	start-stop-daemon -K -q -x /sbin/wpa_supplicant

	echo_info "Remove IP address from $net_dev"
	ip address delete dev $net_dev ${CNET}.1/24

	echo_info "Bring down $net_dev"
	ip link set $net_dev down

	echo_info "WLAN AP stopped"
	rm -f "$WLANAP_MODE_FLAG"
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		stop
		start
		;;
	*)
		echo "Usage: $0 {start|stop|restart}" >&2
		exit 1
		;;
esac

exit 0
